package com.zjr.system.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 加密算法类
 */
public class BCryptUtil {
    /**
     * 生成加密后的密码，生成随机盐值，再给 password 加密
     * @param password 用户输入的密码
     * @return 加密后的密码
     */
    public static String encryptPassword(String password){
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }

    /**
     * 根据 encodePassword 查出盐值，再给 rawPassword 加密，最后比对
     * @param rawPassword 真实的密码
     * @param encodePassword 加密后的密码
     * @return
     */
    public static boolean matchesPassword(String rawPassword, String encodePassword){
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword, encodePassword);
    }

    public static void main(String[] args) {
        System.out.println(encryptPassword("123456"));
        System.out.println(matchesPassword("123456", "$2a$10$368MAtr1TGeKHrHKxoMVnutCI6F0/JrKj0vwQWvRS7VQoka2Bhc8O"));
    }
}
